home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / msysjour / vol07 / 05 / 32gdi / bezier.c next >
C/C++ Source or Header  |  1992-09-01  |  4KB  |  130 lines

  1. /*---------------------------------------
  2.    BEIZER.C -- Win32 Bezier Splines Demo
  3.                (c) Charles Petzold, 1992
  4.   ---------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. LONG APIENTRY WndProc (HWND, UINT, DWORD, LONG) ;
  9.  
  10. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  11.                     LPSTR lpszCmdParam, int nCmdShow)
  12.      {
  13.      static char szAppName[] = "Bezier" ;
  14.      HWND        hwnd ;
  15.      MSG         msg ;
  16.      WNDCLASS    wndclass ;
  17.  
  18.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  19.      wndclass.lpfnWndProc   = WndProc ;
  20.      wndclass.cbClsExtra    = 0 ;
  21.      wndclass.cbWndExtra    = 0 ;
  22.      wndclass.hInstance     = hInstance ;
  23.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  24.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  25.      wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  26.      wndclass.lpszMenuName  = NULL ;
  27.      wndclass.lpszClassName = szAppName ;
  28.  
  29.      RegisterClass (&wndclass) ;
  30.  
  31.      hwnd = CreateWindow (szAppName, "Bezier Splines",
  32.                           WS_OVERLAPPEDWINDOW,
  33.                           CW_USEDEFAULT, CW_USEDEFAULT,
  34.                           CW_USEDEFAULT, CW_USEDEFAULT,
  35.                           NULL, NULL, hInstance, NULL) ;
  36.  
  37.      ShowWindow (hwnd, nCmdShow) ;
  38.      UpdateWindow (hwnd) ;
  39.  
  40.      while (GetMessage (&msg, NULL, 0, 0))
  41.           {
  42.           TranslateMessage (&msg) ;
  43.           DispatchMessage (&msg) ;
  44.           }
  45.      return msg.wParam ;
  46.      }
  47.  
  48. void DrawBezier (HDC hdc, POINT apt [])
  49.      {
  50.      PolyBezier (hdc, apt, 4) ;
  51.  
  52.      MoveToEx (hdc, apt[0].x, apt[0].y, NULL) ;
  53.      LineTo   (hdc, apt[1].x, apt[1].y) ;
  54.  
  55.      MoveToEx (hdc, apt[2].x, apt[2].y, NULL) ;
  56.      LineTo   (hdc, apt[3].x, apt[3].y) ;
  57.      }
  58.  
  59. LONG APIENTRY WndProc (HWND hwnd, UINT message, DWORD wParam, LONG lParam)
  60.      {
  61.      static POINT apt [4] ;
  62.      HDC          hdc ;
  63.      PAINTSTRUCT  ps ;
  64.      short        cxClient, cyClient ;
  65.  
  66.      switch (message)
  67.           {
  68.           case WM_SIZE:
  69.                cxClient = LOWORD (lParam) ;
  70.                cyClient = HIWORD (lParam) ;
  71.  
  72.                apt[0].x = cxClient / 4 ;
  73.                apt[0].y = cyClient / 2 ;
  74.  
  75.                apt[1].x = cxClient / 2 ;
  76.                apt[1].y = cyClient / 4 ;
  77.  
  78.                apt[2].x =     cxClient / 2 ;
  79.                apt[2].y = 3 * cyClient / 4 ;
  80.  
  81.                apt[3].x = 3 * cxClient / 4 ;
  82.                apt[3].y =     cyClient / 2 ;
  83.  
  84.                return 0 ;
  85.  
  86.           case WM_MOUSEMOVE:
  87.                if (wParam & MK_LBUTTON || wParam & MK_RBUTTON)
  88.                     {
  89.                     hdc = GetDC (hwnd) ;
  90.  
  91.                     SelectObject (hdc, GetStockObject (WHITE_PEN)) ;
  92.                     DrawBezier (hdc, apt) ;
  93.  
  94.                     if (wParam & MK_LBUTTON)
  95.                          {
  96.                          apt[1].x = LOWORD (lParam) ;
  97.                          apt[1].y = HIWORD (lParam) ;
  98.                          }
  99.  
  100.                     if (wParam & MK_RBUTTON)
  101.                          {
  102.                          apt[2].x = LOWORD (lParam) ;
  103.                          apt[2].y = HIWORD (lParam) ;
  104.                          }
  105.  
  106.                     SelectObject (hdc, GetStockObject (BLACK_PEN)) ;
  107.                     DrawBezier (hdc, apt) ;
  108.                     ReleaseDC (hwnd, hdc) ;
  109.                     }
  110.  
  111.                return 0 ;
  112.  
  113.           case WM_PAINT:
  114.                InvalidateRect (hwnd, NULL, TRUE) ;
  115.  
  116.            hdc = BeginPaint (hwnd, &ps) ;
  117.  
  118.                DrawBezier (hdc, apt) ;
  119.  
  120.            EndPaint (hwnd, &ps) ;
  121.                return 0 ;
  122.  
  123.           case WM_DESTROY:
  124.                PostQuitMessage (0) ;
  125.                return 0 ;
  126.           }
  127.  
  128.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  129.      }
  130.